package com.cn.forum.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.forum.model.Article;
import com.cn.forum.model.User;
import com.cn.forum.service.ArticleService;
@Controller
public class ArticleController {
	
	@Resource
    private ArticleService articleService;
	
	
	@RequestMapping("doPublish")
	public String publish(HttpServletResponse response,Article article,Model model,HttpSession session)
	{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		User usr=(User)session.getAttribute("usr");
		//session.setAttribute("art", article);
		article.setId(usr.getUserid());
		article.setUsername(usr.getUsername());
		articleService.add(article);
		return "redirect:"+"articleList";
	}
	
	@RequestMapping("/doArticleEdit")
	public String doArticleEdit(HttpServletResponse response,Article article,Model model,HttpSession session)
	{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		User usr=(User)session.getAttribute("usr");
		//注意一定要把article的id传过来啊啊啊不然怎么修改content！
		articleService.change(article);
		return "redirect:"+"articleList";	
	}
	
	
	
	@RequestMapping("doReply")
	public String reply(HttpServletResponse response,Article article,Model model,HttpSession session)
	{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		User usr=(User)session.getAttribute("usr");
		article.setId(usr.getUserid());
		article.setUsername(usr.getUsername());
		article.setUserid(usr.getUserid());
		//article.setParentId(parentArticle.getParentId());
		articleService.add(article);
		return "redirect:"+"articleList";
	}
	
	
	@RequestMapping("/getMyArticle")
	public String getAllMyArticle(Model model,HttpSession session,Integer pageNum,Integer pageSize)
	{
		if(pageNum==null||pageNum<1)
		{
			pageNum=1;
		}
		if(pageSize==null||pageSize<1)
		{
			pageSize=3;
		}
		User user1=(User)session.getAttribute("usr");

		 Integer myArticleNum=articleService.getMyArticleNum(user1.getUsername());
	 	 Integer maxPage=myArticleNum/pageSize;
	 	  if((myArticleNum%pageSize)!=0)
	 	  {
	 		  maxPage=maxPage+1;
	 	  }
	 	  if(pageNum>maxPage){
	 		  pageNum=maxPage;
	 	  }	
		List<Article> myArticle=articleService.getPageMyArticle(user1.getUsername(),pageNum,pageSize);
		
		model.addAttribute("myArticle", myArticle);
		model.addAttribute("username", user1.getUsername());
    	model.addAttribute("myPageNum", pageNum);
    	model.addAttribute("myMaxPage",maxPage);
		return "myArticle";
	}
	
	
	@RequestMapping("articleList")
    public String getPageArticle(Model model,HttpSession session,Integer pageNum,Integer pageSize){
		if(pageNum==null||pageNum<1)
		{
			pageNum=1;
		}
		if(pageSize==null||pageSize<1)
		{
			pageSize=3;
		}
		
		  Integer mainArticleNum=articleService.getMainArticleNum();
	 	  Integer maxPage=mainArticleNum/pageSize;
	 	  if((mainArticleNum%pageSize)!=0)
	 	  {
	 		  maxPage=maxPage+1;
	 	  }
	 	  if(pageNum>maxPage){
	 		  pageNum=maxPage;
	 	  }	
    	List<Article> articleList=articleService.getPageArticle(pageNum,pageSize);
    	List<Article> childrenArticleListAll=articleService.getChildrenArticle(articleList);
		 //做一个parentid和list<article>相对应的map
		  Map<Integer, List<Article>> replyMap = new HashMap<>();
	 	  for (Article a:childrenArticleListAll){
	 		  List<Article> list = replyMap.get(a.getParentId());
	 		  if(list == null) {
	 			  list=new ArrayList<Article>();
	 			  replyMap.put(a.getParentId(),list);
	 		  }
	 		  list.add(a);
	 	  }
	 	  for(Article a:articleList){
	 		  if(replyMap.containsKey(a.getId()))
	 		  {
	 			  List<Article> childList=replyMap.get(a.getId());
	 			  a.setChild(childList);  
	 		  }
	 	  }
    	
    	
    	User user1=(User)session.getAttribute("usr");
    	model.addAttribute("articlelist", articleList);
    	model.addAttribute("username", user1.getUsername());
    	model.addAttribute("pageNum", pageNum);
    	model.addAttribute("maxPage",maxPage);
    	//model.addAttribute("totalPage",totalPage);
    	return "articleList";
  }		
	
	@RequestMapping("/doEdit")
	public String doEdit(Model model,HttpSession session,Article article){
    	User user1=(User)session.getAttribute("usr");
    	if((user1.getUsername()).equals(article.getUsername()))
    	{
    		return "editArticle";
    	}
    	else{
    		return "noPowerToEdit";
    	}
	}

}
